<template>
	<view class="content">
		<image src="https://kehu-1256834579.cos.ap-shanghai.myqcloud.com/weier/login_img.jpg" mode="widthFix" class="login-img"></image>
		<div class="login-btns cl">
			<span class="login-btn" @tap="sub_login()">微信登录</span>
			<div class="login-txt cl">
				<image src="/static/images/dui1.png" mode="widthFix" v-if="if_agree == 0" @tap="open_agree()"></image>
				<image src="/static/images/dui2.png" mode="widthFix" v-else @tap="open_agree()"></image>
				<div class="cl">同意<span>《中国移动认证服务条款》</span><span>《法律条款与隐私政策》</span>并授权蔚尔助行获取本机号码</div>
			</div>
		</div>
		<div class="member-wind" v-if="show_login == 1">
			<div class="member-wind" @tap="open_member(0)"></div>
			<div class="member-wind-inner cl">
				<span class="iconfont icon-guanbi" @tap="open_member(0)"></span>
				<div class="member-round cl" v-if="!avatarUrl">
					<button type="balanced" open-type="chooseAvatar" @chooseavatar="onChooseavatar" class="upload-btns">
						<image src="../../static/images/me1.png" class="photo-member"></image>
						<span class="photo-txt">点击获取头像</span>
					</button>
				</div>
				<div class="member-round cl" v-else>
					<button type="balanced" open-type="chooseAvatar" @chooseavatar="onChooseavatar" class="upload-btns">
						<image :src="avatarUrl" class="photo-member"></image>
					</button>
				</div>
				<div class="nick-round cl">
					<input type="nickname" placeholder="点击获取昵称" placeholder-style="font-size:28rpx" :value="nickName" @input="onKeyEvent($event,'nickName')">
				</div>
				<div class="nick-round cl">
					<button open-type="getPhoneNumber" @getphonenumber="getPhoneNumber" v-if="!mobile" style="font-size: 28rpx;">授权手机号</button>
						<input placeholder-style="font-size:28rpx" :value="mobile" disabled="" v-else>
				</div>
				<span class="login-member" @tap="add_user()">登录</span>
			</div>
		</div>
	</view>
</template>

<script>
	import WXBizDataCrypt from "@/static/js/WXBizDataCrypt.js";
	import common_config from '../../static/js/common.js';
	var config = common_config.config;
	var access_token = uni.getStorageSync('access_token');
	var that;	
	var userInfo;
	export default {
		data() {
			return {
				if_agree:0,
				show_login:0,
				avatarUrl:'',
				nickName:'',
				openid:'',
				session_key:'',
				if_bind:0,
				mobile:''
			}
		},
		onLoad() {
			that = this;
			that.weixin_login();
		},
		methods: {
			 // 选择头像
			onChooseavatar(e) {
			    // #ifdef MP-WEIXIN
			    const { avatarUrl } = e.detail || {};
			    if (!avatarUrl) {
			      uni.showToast({ title: '未选择头像', icon: 'none' });
			      return;
			    }
			    // 把临时路径赋值到 data 里，用于页面显示
			    this.avatarUrl = avatarUrl;
			    console.log('临时头像路径:', avatarUrl);
			    // #endif
			},
			//表单输入事件
			onKeyEvent:function(event,the_id){
				that[the_id] = event.target.value;
			},
			//微信登录
			weixin_login(){
				uni.showLoading({
					title:'正在初始化...'
				});
				uni.login({
				    provider: 'weixin',
				    success: function (loginRes) {
						console.log(loginRes);
						//that.openid = loginRes.code;
						uni.hideLoading();
						uni.request({
						    url: config.weixin_url+'/api/Login/GetWxUserInfo?code='+loginRes.code,
						    header: {
						         "Content-Type": "application/json",
						    },
							method:'GET',
						    success: (res) => {
								uni.hideLoading();
								res = res.data;
						        if(res.msg == '成功'){
									that.openid = res.response.openid;
									console.log(that.openid);
								}
						    }
						});
						
				        
				    },
				    fail: function (err) {
						uni.hideLoading();
				        // 登录授权失败  
				        // err.code是错误码
				    }
				});
			},
			load_row_id(){
				uni.request({
				  url: config.request_url+'/v2/open/worksheet/getFilterRows',
				  method: 'POST',
				  header: {
					'Content-Type': 'application/json'
				  },
				  data: {
					"appKey": config.appKey,
					"sign": config.sign,
					"worksheetId": "yhgl",
					"pageSize": 50,
					"pageIndex": 1,
					"filters": [
						{
							"controlId": "openid",
							"dataType": 2,
							"spliceType": 1,
							"filterType": 2,
							"value": that.openid //传openid
						}
					]
				  },
				  success: (res) => {
					console.log('返回结果:', res.data);
					res = res.data;
					console.log(res);
					if(res.success){
						var datas = res.data.rows;
						for(var i = 0;i < datas.length;i++){
							if(datas[i].openid == that.openid){
								
								var member_profile = {
									openid:datas[i].openid,
									nickName:datas[i].nicheng,
									photo:'',
									rowid:datas[i].rowid,
									mobile:datas[i].shoujihao,
									xinyongfen:datas[i].xinyongfen,
									yajin:datas[i].yajin,
									sfsx:datas[i].sfsx,
								};
								uni.setStorageSync('userInfo',JSON.stringify(member_profile));
								uni.showToast({ title: '登录成功', icon: 'success' });
								uni.switchTab({
									url:'/pages/index/me'
								});
								return false;
							}
						}
						that.open_member(1);
					}else{
						 uni.showToast({ title: res.data.msg || '数据获取失败', icon: 'none' });
					}
				  },
				  fail: (err) => {
					console.error('请求失败:', err);
					uni.showToast({ title: '网络错误', icon: 'none' });
				  }
				})  
			},
			add_user(){
				if(!that.openid){
					common_config.calert('请先同意用户授权');
					return false;
				}
				if(!that.nickName){
					common_config.calert('请选择用户昵称');
					return false;
				}
				if(!that.openid){
					common_config.calert('请选择您的头像');
					return false;
				}
				if(!that.mobile){
					common_config.calert('请输入您的手机号');
					return false;
				}
				uni.request({
				  url: config.request_url+'/v2/open/worksheet/addRow',
				  method: 'POST',
				  header: {
				    'Content-Type': 'application/json'
				  },
				  data: {
				    "appKey": config.appKey,
				    "sign": config.sign,
				    "worksheetId": "68abc8681701191c066abfe3",
				    "controls": [
						{
						  "controlId": "openid",
						  "value": that.openid
						},
				      {
				        "controlId": "shoujihao",
				        "value": that.mobile
				      },
				      {
				        "controlId": "nicheng",		//昵称
				        "value": that.nickName
				      },
				      {
				        "controlId": "touxiang",
				        "value": that.avatarUrl
				      }
				    ]
				  },
				  success: (res) => {
				    console.log('返回结果:', res.data);
					res = res.data;
					if(res.success){
						that.load_row_id();
					}else{
						 uni.showToast({ title: res.data.msg || '添加失败', icon: 'none' });
					}
				  },
				  fail: (err) => {
				    console.error('请求失败:', err);
				    uni.showToast({ title: '网络错误', icon: 'none' });
				  }
				})

			},
			//判断是否已注册
			sub_login(){
				if(!that.if_agree){
					common_config.calert('请先阅读并同意《中国移动认证服务条款》和《法律条款与隐私政策》');
					return false;
				}
				that.load_row_id();
				//that.open_member(1);
			},
			getPhoneNumber(PhoneNumber) {
				uni.request({
				    url: 'https://zsnpsy.sstms.net/weier/api/Login/GetWxUserPhoneNumber',
				    header: {
				         "Content-Type": "application/json"
				    },
					method:'GET',
					data:{code:PhoneNumber.detail.code},
				    success: (res) => {
						uni.hideLoading();
						res = res.data;
				        if(res.msg != '成功'){
							common_config.calert(res.msg);
				        }else{
							console.log(res.response);
							that.mobile = res.response.phoneNumber;
							
				        }
				    }
				});
			},
			param_to_url(url){
				uni.navigateTo({
					url:url
				})
			},
			open_member(n){
				that.show_login = n;
			},
			open_agree(){
				this.if_agree = this.if_agree == 1 ? 0 : 1;
			}
		}
	}
</script>

<style>
.login-img{width: 100%;}
.login-btn{background-color: #07C160;width: 90%;margin-left: 5%;text-align: center;display: block;border-radius: 36rpx;color: #fff;padding: 20rpx 0px;margin-top: 60rpx;}
.login-txt{width: 90%;margin-left: 5%;margin-top: 20rpx;}
.login-txt image{float: left;width: 34rpx;}
.login-txt div span{font-size: 28rpx;}
.login-txt div{color: #8D8D8D;font-size: 28rpx;float: left;width: 90%;margin-left: 10rpx;margin-top: -4rpx;}
/*用户信息弹窗*/
.member-wind{width: 100%;height: 100%;left: 0px;top: 0px;position: fixed;background-color: rgba(0,0,0,0.3);z-index: 10;}
.member-wind-inner{position: fixed;bottom: 0px;background-color: #fff;width: 94%;padding: 20rpx 3%;padding-bottom: 40rpx;z-index: 11;}
.icon-guanbi{position: absolute;right: 20rpx;top: 20rpx;font-size: 46rpx;z-index: 12;}
.upload-btns{width: 30%;margin-left:35%;border: 0px;background-color: #fff;border-right: 1px solid #ddd}
.member-round{margin-top: 20rpx;text-align: center;width: 100%;position: relative;}
.member-round image{border: 1px solid #ddd;border-radius: 100%;margin-top: 20rpx;width: 160rpx;height: 160rpx;}
.member-round .photo-txt{position: absolute;font-size: 28rpx;top: 45%;left:10%;color: #666;}
.nick-round{width: 100%;text-align: center;margin-top: 20rpx;position: relative;}
.nick-round input{background-color: #f3f3f3;padding: 20rpx 2%;border-radius: 6rpx;font-size: 28rpx;width:100%;}
.login-member{margin-top:40rpx;display: inline-block;width: 80%;margin-left:10%;background-color: #07C160;text-align: center;padding:20rpx 0px;color: #fff;border-radius: 12rpx;}
.nick-round .send-code{display: inline-block;background-color: #fff;border:1px solid #07C160;color: #07C160;font-size: 24rpx;border-radius: 6rpx;width: 160rpx;padding:10rpx 0px;text-align: center;display: inline-block;float:left;margin-left: 10rpx;margin-top:12rpx;right:2%;position: absolute;top: 0px;z-index: 10;}
</style>
